/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import core.Activity;
import core.TestData;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;

/**
 *
 * @author Dominik
 */
@Stateless
public class ActivityDAO implements IActivity {

  @Override
  public void createActivity(Activity a) {
    if (a == null) {
      throw new RuntimeException("null paprametr");
    } else {
      Integer userId = this.getMaxActivityId();
      a.setId(userId);
      TestData.ACTS_TABLE.put(userId, a);
    }
  }

  @Override
  public void updateActivity(Activity a) {
    TestData.ACTS_TABLE.put(a.getId(), a);
  }

  @Override
  public void deleteActivity(Activity a) {
    if (a == null) {
      throw new RuntimeException("null");
    }
    TestData.ACTS_TABLE.remove(a.getId());
  }

  @Override
  public Activity findActivity(int aId) {
    return TestData.ACTS_TABLE.get(aId);
  }

  @Override
  public Activity findActivity(String username) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public List<Activity> findAllActivities() {
    return new ArrayList<Activity>(TestData.ACTS_TABLE.values());
  }

  private Integer getMaxActivityId() {
    Set<Integer> keys = TestData.ACTS_TABLE.keySet();
    Integer maxId = 1;
    for (Integer key : keys) {
      if (key > maxId) {
        maxId = key;
      }
    }
    return maxId;
  }
}
